#include "baseresp.h"
#include "../lib/osdjson.h"
#include "remoteerr.h"
#include "remotedef.h"
#include <QVariant>
#include <QDebug>

BaseResp::BaseResp()
{
    code = "";
    m_baseName = "";
    isSuccess = false;

    m_map.insert(RETURN_CODE_INVALID_ACCOUNT_PWD,"无效的账户或密码。");
    m_map.insert(RETURN_CODE_INVALID_TOKEN,"无效的token。");
    m_map.insert(RETURN_CODE_INVALID_PARAM,"无效的参数。");
    m_map.insert(RETURN_CODE_LICENSE_EXPIRED,"授权已失效。");
    m_map.insert(RETURN_CODE_NTP_NORESPONSE,"NTP服务器没有返回。");
    m_map.insert(RETURN_CODE_INTERNAL_ERR,"系统内部错误。");
    m_map.insert(RETURN_CODE_DEVICE_SWITCH,"设备正处于模式切换中。");
    m_map.insert(RETURN_CODE_INVALID_CONF_NUMBER,"当前不存在该会议，请重新输入！");
    m_map.insert(RETURN_CODE_NEED_PWD,"需要输入密码。");
    m_map.insert(RETURN_CODE_INVALID_PWD,"密码有误，请重新输入！");
    m_map.insert(RETURN_CODE_DEVICE_BUSY,"设备正处于忙碌状态。");
    m_map.insert(RETURN_CODE_DEVICE_OFFLINE,"设备未注册SVC服务器。");
    m_map.insert(RETURN_CODE_UP_SERVER_ERR,"升级服务器无法连接。");
    m_map.insert(RETURN_CODE_VNERSION_NEW,"版本已是最新。");
    m_map.insert(RETURN_CODE_UPGRADE_FAILED,"升级失败。");
}

BaseResp::~BaseResp()
{

}

QString BaseResp::getCodeString()
{
    return m_map.value(code,"未知原因");
}
bool BaseResp::getIsSuccess() const
{
    return isSuccess;
}

QString BaseResp::baseName() const
{
    return m_baseName;
}

void BaseResp::setBaseName(const QString &baseName)
{
    m_baseName = baseName;
}



void BaseResp::unserialize(QString json)
{
    OSDJson object(json);
    code = object.value(KEY_RESP_CODE).toString();
    m_json = object.object(KEY_RESP_DATA);
    if (RETURN_CODE_SUCCESS != code)
    {
        qDebug() << "json:" << json;
        isSuccess = false;
        if (m_json.isValid())
        {
            err = m_json.value(KEY_ERR).toString();
        }
    }
    else
    {
        err = "";
        isSuccess = true;
    }
}
QString BaseResp::getCode() const
{
    return code;
}

void BaseResp::setCode(QString code)
{
    err = "";
    isSuccess = true;
    this->code = code;
}

